-- dragon_prediction
-- create by huanghr
-- 施放大预言术

return {
    apply = function(source, target, skillId, round, para, extra)
        local pos = target:getPos();
        -- 随机抽取3类技能，再随机抽取技能

        local function sortId(item1, item2)
            return item1 < item2;
        end
        local skillType = DragonPredictionM.getAllSkillType();
        table.sort(skillType, sortId);

        local count = #skillType;
        if count <= 0 then
            return;
        end

        local arr = {};

        for i = 1, para do
            if count <= 0 then
                break;
            end

            -- 随机一个类型
            local randomSeed = DungeonM.getRandSeed("DRAGON_PREDICTION");
            local index = randomSeed % count + 1;
            local typ = skillType[index];

            skillType[index] = skillType[count];
            count = count - 1;

            local skillIds = DragonPredictionM.getSkillIdsByType(typ);

            local randomSeed2 = DungeonM.getRandSeed("DRAGON_PREDICTION");
            index = randomSeed2 % #skillIds + 1;

            if #table.keys(skillIds) > 1 then
                table.sort(skillIds, sortId);
            end

            local id = skillIds[index];

            local msg = string.format("[dragon_prediction]抽取大预言术：%d", id);
            DungeonLogM.addLog(msg);

            local skillId = DragonPredictionM.query(id, "skill_id");

            table.insert(arr, skillId);

            EventMgr.fire(event.DRAGON_PREDICTION, { ["skill_id"] = skillId, ["pos"] = pos, ["index"] = i, ["last"] = (i == para) });
        end

        ME.user.dbase:setTemp("dragon_pre_skills", arr);
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        assert(#arr == 1);
        return tonumber(arr[1]);
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local base = para;

        desc = string.gsub(desc, "{prediction}", base);

        return desc;
    end,
}
